<?php
FLEA::loadClass('Controller_Base');

class Controller_TradeMark extends Controller_Base
{
	
	function	actionRecordTradeMark()
	{
		$_modelTradeMark	=&	FLEA::getSingleton('Model_TradeMark');
		
		$trade 	=	$_POST;
		$trade['Invented']	=	date("Y-m-d", time());
		$tradeID			=	$_modelTradeMark->create($trade);
		
		$tradeID			=	$tradeID>0?1:0;
		$this->showResult($tradeID);
	}
	
	function actionGetTradeMarks()
	{
		$role	=	$_POST['Role'];
		$userID	=	$_POST['UserID'];

		$_modelTradeMark	=&	FLEA::getSingleton('Model_TradeMark');
		$marks				=	$_modelTradeMark->findAllTradeMarks($userID,$role);
		if(is_array($marks))
		{
			$this->purse($marks,'marks',0);
		}
	}
	
	function	actionGetBidTradeMarks()
	{
		
		$_modelTradeMark	=&	FLEA::getSingleton('Model_TradeMark');
		$marks				=	$_modelTradeMark->findBidTradeMarks($_POST['FirmID']);

		if(is_array($marks))
		{
			$this->purse($marks,'marks',0);
		}
	}
	
	function actionGetTradeMark()
	{
		$tradeMarkID	=	$_POST['TradeMarkID'];
		$tradeMarkID	=	$tradeMarkID==''?0:$tradeMarkID;
		
		
		$_modelTradeMark	=&	FLEA::getSingleton('Model_TradeMark');
		$mark				=	$_modelTradeMark->findTradeMark($tradeMarkID,$this->getInt($_POST['UserID'])
									,$this->getInt($_POST['Role']));
		
		if(is_array($mark))
		{
			$this->purse($mark,'mark',0);
		}
	}
	
	function actionGetTradeMarkActions()
	{
		$tradeMarkID	=	$_POST['TradeMarkID'];
		$tradeMarkID	=	$tradeMarkID==''?0:$tradeMarkID;
		
		$_modelTradeMarkAction	=&	FLEA::getSingleton('Model_TradeMarkAction');
		$actions				=	$_modelTradeMarkAction->findAll('TradeMarkID='.$tradeMarkID);
		
		print '<actions>';
		for($i=0; $i<count($actions) ; $i++)
		{
			print '<action
				label="'.$GLOBALS['ActionType'][$actions[$i]['Type']].'   [ Date:'.$actions[$i]['Date'] .']"
				ActionID="'.$actions[$i]['ActionID'].'"
				Cost="'.$actions[$i]['Cost'].'"
				Type="'.$actions[$i]['Type'].'"
				Date="'.$actions[$i]['Date'].'"
				Description="'.$actions[$i]['Description'].'"
				DueDate="'.$actions[$i]['DueDate'].'">';
			for($j=0; $j<count($actions[$i]['descriptions']); $j++)
			{


				print '<File label="';
				print substr($actions[$i]['descriptions'][$j]['Drawing0'],8,strlen($actions[$i]['descriptions'][$j]['Drawing0'])-8);
				print '"></File>';
			}
			print '</action>';
		}
		print '</actions>';
	}
	
	function actionAssignTradeMarkToFirm()
	{
		$_modelTradeMark	=&	FLEA::getSingleton('Model_TradeMark');
		
		$trade	=	$_POST;
		$trade['BConfirm']	=	0;
		$trade['DeadlineDate']	=	date("Y-m-d",strtotime($_POST['DeadlineDate']));
		$result	=	$_modelTradeMark->save($trade);
		$this->showResult($result);
	}
	
	function	actionAcceptAssignMark()
	{
		$_modelTradeMark	=&	FLEA::getSingleton('Model_TradeMark');
		
		$trade['TradeMarkID']	=	$_POST['TradeMarkID'];
		$trade['BConfirm']		=	$_POST['BConfirm'];
		$trade['FirmID']		=	$_POST['FirmID'];
		
		
		$result	=	$_modelTradeMark->save($trade);
		
		if($trade['BConfirm'] == 1)
		{
			$_modelActionEvent	=&	FLEA::getSingleton("Model_ActionEvent");
			$_modelActionEvent->updateActionEvent($_POST['FirmID'],$_POST['ClientID'],"Accept trademark");
		}
		
		$this->showResult($result);
	}
	
	function actionAddAction()
	{
		$_modelTradeMark		=&	FLEA::getSingleton('Model_TradeMark');
		$_modelTradeMarkAction	=&	FLEA::getSingleton('Model_TradeMarkAction');
		$_modelDescription		=&	FLEA::getSingleton('Model_Description');

		$action1['Title']				=	$_POST['Title']!=''?$_POST['Title']:'';
		$action1['Type']				=	$_POST['Type'];
		$action1['Description']			=	$_POST['Description'];
		$action1['TradeMarkID']			=	$_POST['TradeMarkID']==''?0:$_POST['TradeMarkID'];
		$action1['Date']				=	date("Y-m-d", time());
		$action1['Cost']				=	$_POST['Cost'];

		$actionID	=	$_modelTradeMarkAction->create($action1);

		if($_POST['FileNames'] != "")
		{
			$files	=	split("-",$_POST['FileNames']);

			for($k=0; $k<count($files); $k++)
			{
				if($files[$k] != "")
				{
					$description['ActionID']	=	$actionID;
					$description['Date']		=	date("Y-m-d", time());
					$description['Drawing0']	=	"Drawing/".$files[$k];
					$_modelDescription->create($description);
				}
			}
		}
		$tradeMark['TradeMarkID']	=	$tradeMark['InventionID'];
		$tradeMark['LastActionDate']=	$action1['Date'];
		$_modelTradeMark->save($tradeMark);
		$this->showResult($actionID>0?1:0);
	}
}
?>